<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\ShopOrderReturn\ShopOrderReturnAllGoodsAction;
use App\Admin\Actions\ShopOrderReturn\ShopOrderReturnAllOnlyAction;
use App\Admin\Actions\ShopOrderReturn\ShopOrderReturnConfirmAction;
use App\Admin\Actions\ShopOrderReturn\ShopOrderReturnPartGoodsAction;
use App\Admin\Actions\ShopOrderReturn\ShopOrderReturnPartOnlyAction;
use App\Admin\Actions\ShopOrderReturn\ShopOrderReturnRejectAction;
use App\Admin\Grid\NGrid;
use App\Models\ShopOrderReturnModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ShopOrderReturnController extends AdminController
{
    protected $title = '售后';

    public function grid(): NGrid
    {
        $grid = new NGrid(new ShopOrderReturnModel);
        $grid->model()->leftJoin('shop_order', 'shop_order.order_id', 'shop_order_return.order_id')
            ->leftJoin('shop_order_ext', 'shop_order_ext.order_id', 'shop_order_return.order_id')
            ->leftJoin('shop_user as user', 'user.user_id', 'shop_order_return.buy_user_id')
            ->leftJoin('shop_user as seller', 'seller.user_id', 'shop_order_return.sell_user_id')
            ->select(['shop_order_return.*', 'status', 'refund_at', 'user.user_mobile', 'seller.user_mobile as seller_mobile', 'shop_order.serial_number as orderNumber'])->orderByDesc('or_id');
        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('or_id', 'ID')->integer();
                $filter->equal('shop_order_return-serial_number', __('shop_order_return.serialNumber'));
                $filter->equal('shop_order-serial_number', __('shop_order_return.orderNumber'));
                $filter->equal('user-user_mobile', __('shop_order_return.userMobile'));
                $filter->equal('or_reason', __('shop_order_return.reason'))->select(__('shop_order_return.reasonArr'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('or_type', __('shop_order_return.type'))->select(__('shop_order_return.typeArr'));
                $filter->equal('or_buy_status', __('l.status'))->select(__('shop_order_return.statusArr'));
                $filter->equal('seller-user_mobile', __('shop_order_return.sellerMobile'));
                $filter->between('shop_order_return-add_at', __('l.createdAt'))->datetime();
            });
        });
        $grid->column('or_id', 'ID')->sortable();
        $grid->column('serial_number', __('shop_order_return.serialNumber'));
        $grid->column('orderNumber', __('shop_order_return.orderNumber'));
        $grid->column('or_type', __('shop_order_return.type'))->using(__('shop_order_return.typeArr'));
        $grid->column('or_buy_status', __('l.status'))->using(__('shop_order_return.statusArr'));
        $grid->column('user_mobile', __('shop_order_return.userMobile'));
        //$grid->column('seller_mobile', __('shop_order_return.sellerMobile'));
        $grid->column('or_reason', __('shop_order_return.reason'))->using(__('shop_order_return.reasonArr'));
        $grid->column('or_price', __('shop_order_return.price'));
        $grid->column('or_real_price', __('shop_order_return.refundPrice'));
        $grid->column('or_ensure_price', __('shop_order_return.ensure_price'));
        $grid->column('img_id_json', __('shop_order_return.proof'))->display(function ($column) {
            $images = is_null($column) ? null : json_decode($column, true);
            return view('file_ids', ['id' => $this->or_id, 'images' => $images]);
        });
        $grid->column('or_approved_time', __('shop_order_return.orApprovedTime'))->display(function ($column) {
            return $column === 0 ? null : now()->timestamp($this->or_approved_time)->toDateTimeString();
        });
        $grid->column('refund_at', __('shop_order_return.refundAt'));
        $grid->column('created_at', __('l.createdAt'));
        $grid->disableActions(false);
        $grid->actions(function (Actions $action) {
            $action->disableDelete();
            $action->disableEdit();
            if ($action->row->or_buy_status === 1) {
                if ($action->row->or_type === 2) {//退货类型，1=退货退款，2=仅退款
                    $action->add(new ShopOrderReturnAllOnlyAction);
                    $action->add(new ShopOrderReturnPartOnlyAction);
                } else {
                    $action->add(new ShopOrderReturnAllGoodsAction);
                    $action->add(new ShopOrderReturnPartGoodsAction);
                }
                $action->add(new ShopOrderReturnRejectAction);
            } else if ($action->row->or_buy_status === 4) $action->add(new ShopOrderReturnConfirmAction);
        });
        return $grid;
    }

    /**
     * detail
     * @param int $id
     * @return Show
     */
    protected function detail(int $id): Show
    {
        $show = new Show(ShopOrderReturnModel::query()
            ->leftJoin('shop_order_ext', 'shop_order_ext.order_id', 'shop_order_return.order_id')
            ->leftJoin('shop_order_goods', 'shop_order_goods.order_id', 'shop_order_ext.order_id')
            ->leftJoin('shop_user as user', 'user.user_id', 'shop_order_return.buy_user_id')
            ->leftJoin('shop_user as seller', 'seller.user_id', 'shop_order_return.sell_user_id')
            ->find($id, [
                'shop_order_return.*', 'status', 'refund_at',
                'user.user_mobile', 'seller.user_mobile as seller_mobile',
                'shop_order_goods.title',
                'shop_order_goods.spec',
                'shop_order_goods.product_file_id',
                'shop_order_goods.spec_file_id'
            ]));
        $show->field('or_id', 'ID');
        $show->field('serial_number', __('l.serialNumber'));
        $show->field('product_file_id', __('shop_order_return.productFileId'))->unescape()->as(function ($field) {
            return view('file_id', ['id' => 'productFileId', 'image' => fileIdToUrl($field)]);
        });
        $show->field('spec_file_id', __('shop_order_return.specFileId'))->unescape()->as(function ($field) {
            return view('file_id', ['id' => 'specFileId', 'image' => fileIdToUrl($field)]);
        });
        $show->field('title', __('shop_order_return.title'));
        $show->field('spec', __('shop_order_return.spec'));
        $show->field('user_mobile', __('shop_order_return.userMobile'));
        $show->field('seller_mobile', __('shop_order_return.sellerMobile'));
        $show->field('order_id', __('shop_order_return.order_id'));
        $show->field('order_id', __('shop_order_return.order_id'))->unescape()->as(function ($field) {
            return view('href', ['title' => $field, 'href' => '/admin/shopOrder/' . $field]);
        });
        $show->field('status', __('l.status'))->using(__('shop_order_return.statusArr'));
        $show->field('or_type', __('shop_order_return.type'))->using(__('shop_order_return.typeArr'));
        $show->field('or_reason', __('shop_order_return.reason'))->using(__('shop_order_return.reasonArr'));
        $show->field('or_sn', __('shop_order_return.or_sn'));
        $show->field('add_user_id', __('l.add_user_id'));
        $show->field('edit_user_id', __('l.edit_user_id'));
        $show->field('or_num', __('shop_order_return.num'));
        $show->field('or_price', __('shop_order_return.price'));
        $show->field('or_real_price', __('shop_order_return.refundPrice'));
        $show->field('or_ensure_price', __('shop_order_return.ensure_price'));
        $show->field('or_note', __('shop_order_return.remark'));
        $show->field('or_api_order_sn', __('shop_order_return.api_order_sn'));
        $show->field('or_api_status', __('shop_order_return.api_status'));
        $show->field('or_api_time', __('shop_order_return.api_time'))->unescape()->as(function ($field) {
            return now()->timestamp($field)->toDateTimeString();
        });
        $show->field('or_approved_time', __('shop_order_return.approved_time'))->unescape()->as(function ($field) {
            return now()->timestamp($field)->toDateTimeString();
        });
        $show->field('or_sell_reject_remark', __('shop_order_return.sell_reject_remark'));
        $show->field('or_is_delete', __('l.delete'))->using(__('l.deleteWhether'));
        $show->field('or_add_time', __('shop_order_return.add_time'))->unescape()->as(function ($field) {
            return now()->timestamp($field)->toDateTimeString();
        });
        $show->field('or_contacts', __('shop_order_return.contacts'));
        $show->field('or_contacts_mobile', __('shop_order_return.contacts_mobile'));
        $show->field('or_contacts_address', __('shop_order_return.contacts_address'));
        $show->field('img_id_json ', __('shop_order_return.proof'))->unescape()->as(function ($field) {
            return view('file_ids', ['id' => 'certification', 'images' => $field]);
        });

        $show->field('refund_at', __('shop_order_return.refundAt'))->unescape()->as(function () {
            return $this->refund_at;
        });
        $show->field('add_at', __('l.createdAt'));
        $show->field('order_goods_ids', __('shop_order_return.order_goods_ids'))->unescape()->as(function ($field) {
            return view('json', ['id' => 'order_goods_ids', 'json' => $field]);
        });
        $show->field('or_api_callback_json', __('shop_order_return.api_callback_json'))->unescape()->as(function ($field) {
            return view('json', ['json' => json_decode($field)]);
        });
        return $show;
    }

    public function paging(Content $content): Content
    {
        return $content->title($this->title())->description($this->description['index'] ?? trans('admin.list'))
            ->body(view('orderReturn.paging', [
                'newAuth' => aesEncrypt(encodeId(Admin::user()->user_id)),
            ]));
    }
}
